package leetcode.array;

import java.util.Arrays;
import java.util.HashMap;

/**
 * @author mazouri
 * @create 2021-05-02 23:03
 */
public class TwoSum_hashmap {
    public static int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int expectNum = target - nums[i];
            if (map.containsKey(expectNum)){
                return new int[]{map.get(expectNum),i};
            }else {
                map.put(nums[i],i);
            }
        }
        return new int[2];
    }

    public static void main(String[] args) {
        int[] nums = {2, 7, 5, 15};
        System.out.println(Arrays.toString(twoSum(nums, 7)));
    }
}
